<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ChatRoom.aspx.vb" Inherits="ChatRoom" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Chat System</title>
    <script language="javascript" src="ajax/common.ashx"></script>
    <script language="javascript" src="ajax/BA.Chat.ChatEngine,ChatEngine.ashx"></script>
    <script language="javascript" src="ajax/BA.Chat.ChatEngineUser,ChatEngineUser.ashx"></script>

    <script language="JavaScript">
        
        //No right click code start
        var oLastBtn=0;
        bIsMenu = false;
        
        if (window.Event) 
        {
            document.captureEvents(Event.MOUSEUP); 
        }
        function nocontextmenu()
        { 
            event.cancelBubble = true 
            event.returnValue = false; 
            return false; 
        } 
        function norightclick(e) 
        { 
            if (window.Event) 
            { 
            if (e.which !=1) 
                return false; 
            } 
            else 
            if (event.button !=1) 
            { 
                event.cancelBubble = true 
                event.returnValue = false; 
                return false; 
            } 
        } 
        document.oncontextmenu = nocontextmenu; 
        document.onmousedown = norightclick; 
        //no right click end
        
        //ajax using C# function to get the chat content
        function dameMsg_CallBack(response){ 
            document.getElementById('ChatContent').innerHTML = response.value;
            setTimeout("ChatEngine.GetAllMessages(dameMsg_CallBack)",1000);
        }
        
        function cleanTxt(){
            document.getElementById('txtMsg').value = '';
        }
        
        //ajax using C# function to get the online users
        //the idea is from http://www.codeproject.com/useritems/simplechat.asp Autor: dealarconjose
        function customer_CallBack(response)
        {                     
            document.getElementById('CustomerContent').innerHTML = response.value;
            setTimeout("ChatEngineUser.GetAllUsers(customer_CallBack)",1000);
        }
        
        function username(){          
            var uname='<%# Session("UserName").ToString() %>';     
            document.getElementById('HidedUserName').value = uname;  
        }
        
        //auto scrolling the chat content
        function scroll(){
            if(document.getElementById('ckAutoScroll').checked==true)
            {
                var objDiv = document.getElementById('ChatContent');
                objDiv.scrollTop = objDiv.scrollHeight;
            }
            setTimeout("scroll()",1);
        }
        
        function load() {
            window.location="ChatRoom.aspx";
        }
        
        //if close button clicked sign the user out **start**
        var UserClicked=false;
        document.onmousedown=spyclick;
        function spyclick()
        {
              UserClicked=true;
              setTimeout("UserClicked=false",1);
        }
        
        function popup()
        {
            if(!UserClicked)
            {
                ChatEngineUser.SignOut(document.getElementById('HidedUserName').value);
            }
        }
        
        window.onbeforeunload=popup;
        //**end**
        
        function document.onkeydown(){
            //if alt + F4 pressed sign the user out
            if ((window.event.altKey)&&(window.event.keyCode==115))
            {
                ChatEngineUser.SignOut(document.getElementById('HidedUserName').value);
                return false;
            }
            //block F5, no refresh
            if(window.event.keyCode==116)
            {
                event.keyCode=0;
                event.returnValue = false;
            }
            //block Ctrl + R, no refresh
            if ((window.event.ctrlKey)&&(window.event.keyCode==82))
            {
                event.keyCode=0;
                event.returnValue = false;
            }
            //if enter pressed, press Tab key instead
            if (event.keyCode==13) 
            {
                event.keyCode=9;
                event.returnValue = false; 
            }
            //block ctrl + N, no new window
            if ((window.event.ctrlKey)&&(window.event.keyCode==78))
            {
                event.keyCode=0;
                event.returnValue = false;
            }
        }
    </script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onload="ChatEngine.GetAllMessages(dameMsg_CallBack),scroll(),ChatEngineUser.GetAllUsers(customer_CallBack),username()">
    <form id="form1" runat="server" method="post">

        <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td>
                    <a href="default.aspx"><asp:image id="uxPageTitleImage" runat="server" skinid="pagetitle" /></a>
                </td>
            </tr>
            <tr>
                <td class="TITLECELL">&nbsp;</td>
            </tr>    
        </table>   
                              
        <table width="100%">
            <tr>
                <td valign="top">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td class="CTNTLCELL" nowrap></td>
                            <td class="CTNTMCELL" nowrap>
                                <asp:Panel ID="uxHeaderPanel" runat="server" Height="21px">
                                    <div class="CTNTITLE" style="float:left;"></div>
                                </asp:Panel>
                            </td>
                            <td class="CTNTRCELL" nowrap></td>
                        </tr>
                        <tr>
                            <td class="CTNLSCELL"></td>
                            <td class="CTNBODY">
                                <asp:Panel ID="uxBodyPanel" runat="server">
                                    <table width="100%">
                                        <tr>
                                            <td style="width: 20%;">
                                                <div runat="server" id="CustomerContent" style="BORDER: black 2px solid; height:300px;width:75px;padding:4px;overflow-y:scroll;word-break: break-all;overflow:auto; BACKGROUND-COLOR: #ffffcc;"></div>                                            
                                            </td>                                        
                                            <td style="width: 80%;">
                                                <div runat="server" id="ChatContent" style="BORDER: black 2px solid; height:300px;width:500px;padding:4px;overflow-y:scroll;word-break: break-all;overflow:auto; BACKGROUND-COLOR: #ffffcc;"></div>                                            
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                Message&nbsp;<input type="text" id="txtMsg" name="txtMsg" size="50" onkeydown="if(window.event.keyCode==13) btnSubmit.click()"/>
                                                <!-- ajax using C# funtion of AddMessage() -->
                                                <input id="btnSubmit" type="button" value="Submit" onclick="javascript:ChatEngine.AddMessage(document.getElementById('HidedUserName').value,document.getElementById('txtMsg').value),cleanTxt()"/>
                                                Auto Scroll&nbsp;<input id="ckAutoScroll" type="checkbox" checked /><br />
                                                <input id="HidedUserName" type="hidden" value="" />
                                                <br />
                                                <!-- ajax using C# funtion of Clean() -->
                                                <input id="btnClean" type="button" value="Clear Messages"onclick="javascript:ChatEngine.Clean();"/>
                                                <!-- ajax using C# funtion of SignOut() -->
                                                <input id="Button1" type="button" value="Sign Out"onclick="javascript:ChatEngineUser.SignOut(document.getElementById('HidedUserName').value);javascript:load();javascript:window.close()"/>                                                                       
                                            </td>
                                        </tr>
                                    </table>        
                                </asp:Panel>
                            </td>
                            <td class="CTNRSCELL"></td>
                        </tr>
                        <tr>
                            <td class="CTNBLCELL"></td>
                            <td class="CTNBMCELL"></td>
                            <td class="CTNBRCELL"></td>
                        </tr>        
                    </table>                 
                </td>
            </tr>
        </table>       
    </form>
</body>
</html>
